package com.ohc.leetcode.二叉树.repect;

import com.ohc.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 2022年4月28日23:51:02
 */
public class 二叉树前序遍历01 {

    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        List<Integer> recurse = recurse(root, list);

        return list;
    }

    public static List<Integer> recurse(TreeNode root, List<Integer> list) {
        if (root == null) {
            return null;
        }
        list.add(root.val);
        recurse(root.left, list);
        recurse(root.right, list);
        return list;
    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode();
        TreeNode treeNode2 = new TreeNode();
        TreeNode treeNode3 = new TreeNode();
        treeNode1.val = 2;
        treeNode2.val = 4;
        treeNode3.val = 5;
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode2.left = null;
        treeNode2.right = null;
        treeNode3.left = null;
        treeNode3.right = null;
        List<Integer> list = preorderTraversal(treeNode1);
        list.forEach(e -> {
            System.out.println(e);
        });
    }

}
